class Solution
{
public:
    long long minEnd(int n, int x)
    {
        vector<int> bits(64, 0);
        for (int i = 0; i < 64; i++)
        {
            bits[i] = ((long long)x >> i) & 1;
        }
        int j = 0;
        --n;
        for (int i = 0; i < 64 && j < 64; i++)
        {
            while (bits[j] == 1)
            {
                j++;
            }
            bits[j] |= ((long long)n >> i) & 1;
            j++;
        }
        long long res = 0;
        for (int i = 0; i < 64; i++)
        {
            res |= ((long long)bits[i] << i);
        }
        return res;
    }
};